package com.azxcrm.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 * 
 * @author Dustyone
 */
public class DateUtils {
	public static final String DEFAULT_YYYYMMDD = "yyyyMMddHHmmss";

	public static final String DEFAULT_YYYY_MM_DD = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 获取当前日期, 默认格式为yyyy-MM-dd
	 * 
	 * @return String
	 */
	public static String getDate() {
		return dateTimeNow("yyyy-MM-dd");
	}

	/**
	 * 获取当前日期, 默认格式为yyyyMMdd
	 * 
	 * @return String
	 */
	public static String getDateStamp() {
		return dateTimeNow("yyyyMMdd");
	}

	public static final String dateTimeStr() {
		return dateTimeNow(DEFAULT_YYYY_MM_DD);
	}

	public static final String dateTimeNow() {
		return dateTimeNow(DEFAULT_YYYYMMDD);
	}

	public static final String dateTimeNow(final String format) {
		return dateTime(format, new Date());
	}

	public static final String dateTime(final Date date) {
		return dateTime(DEFAULT_YYYYMMDD, date);
	}

	public static final String dateTime(final String format, final Date date) {
		return new SimpleDateFormat(format).format(date);
	}

	public static final Date dateTime(final String format, final String ts) {
		try {
			return new SimpleDateFormat(format).parse(ts);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 指定日期加上天数后的日期
	 * 
	 * @param num
	 *            为增加的天数
	 * @param newDate
	 *            创建时间
	 * @return
	 * @throws ParseException
	 */
	public static String plusDay(int num, String newDate) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date currdate = format.parse(newDate);
		Calendar ca = Calendar.getInstance();
		ca.setTime(currdate);
		ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
		currdate = ca.getTime();
		String enddate = format.format(currdate);
		System.out.println("增加天数以后的日期：" + enddate);
		return enddate;
	}

	/**
	 * 比较两个日期之间的大小
	 * 
	 * @param DATE1
	 * @param DATE2
	 * @return 前者大于后者返回true 反之false
	 */
	public static boolean compare_date(String DATE1, String DATE2) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = format.parse(DATE1);
		Date date2 = format.parse(DATE2);
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(date1);
		c2.setTime(date2);
		int result = c1.compareTo(c2);
		if (result >= 0)
			return true;
		else
			return false;
	}

	public static long getDaySub(String beginDateStr, String endDateStr) throws ParseException{
		long day = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date beginDate;
		Date endDate;
		
		beginDate = format.parse(beginDateStr);
		endDate = format.parse(endDateStr);
		day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
		// System.out.println("相隔的天数="+day);

		return day;
	}

}
